﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class DMGDBO : Csla.BusinessBase<DMGDBO>
    {
        #region Business Properties

        private int? _GDID = null;
        public int? GDID
        {
            get { return this._GDID; }
            set { this._GDID = value; PropertyHasChanged("GDID"); }
        }

        private int? _NHGD = Session.NHGD;
        public int? NHGD
        {
            get { return this._NHGD; }
            set { this._NHGD = value; PropertyHasChanged("NHGD"); }
        }

        private int? _VTRI = 0;
        public int? VTRI
        {
            get { return this._VTRI; }
            set { this._VTRI = value; PropertyHasChanged("VTRI"); }
        }

        private int? _LDID = null;
        public int? LDID
        {
            get { return this._LDID; }
            set { this._LDID = value; PropertyHasChanged("LDID"); }
        }

        private int? _TDID = null;
        public int? TDID
        {
            get { return this._TDID; }
            set { this._TDID = value; PropertyHasChanged("TDID"); }
        }

        private double? _GIAC = 0;
        public double? GIAC
        {
            get { return this._GIAC; }
            set { this._GIAC = value; PropertyHasChanged("GIAC"); }
        }

        private bool? _HSCL = false;
        public bool? HSCL
        {
            get { return this._HSCL; }
            set { this._HSCL = value; PropertyHasChanged("HSCL"); }
        }

        private bool? _HSHT = true;
        public bool? HSHT
        {
            get { return this._HSHT; }
            set { this._HSHT = value; PropertyHasChanged("HSHT"); }
        }

        private bool? _HSDD = true;
        public bool? HSDD
        {
            get { return this._HSDD; }
            set { this._HSDD = value; PropertyHasChanged("HSDD"); }
        }

        private int? _VBID = null;
        public int? VBID
        {
            get { return this._VBID; }
            set { this._VBID = value; PropertyHasChanged("VBID"); }
        }

        private string _GHIC = null;
        public string GHIC
        {
            get { return this._GHIC; }
            set { this._GHIC = value; PropertyHasChanged("GHIC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; PropertyHasChanged("STT0"); }
        }

        protected override object GetIdValue()
        {
            return _GDID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newGDID = -1;
        private DMGDBO()
        {
            _GDID = newGDID--;
        }

        public DMGDBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public DMGDBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static DMGDBO NewRootDMGDBO()
        {
            return DataPortal.Create<DMGDBO>(new RootCriteria());
        }

        internal static DMGDBO NewChildDMGDBO()
        {
            return DataPortal.Create<DMGDBO>(new ChildCriteria());
        }

        public static DMGDBO GetRootDMGDBO(int _GDID)
        {
            return DataPortal.Fetch<DMGDBO>(new RootCriteria(_GDID));
        }

        public static DMGDBO GetRootDMGDBOByWhere(string where)
        {
            return DataPortal.Fetch<DMGDBO>(where);
        }

        internal static DMGDBO GetChildDMGDBO(SqlDataReader dataReader)
        {
            return new DMGDBO(dataReader);
        }

        public static void DeleteDMGDBO(int _GDID)
        {
            DataPortal.Delete(new RootCriteria(_GDID));
        }

        public static void DeleteDMGDBO(int _GDID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_GDID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int GDID;
            public bool directDelete = false;
            public RootCriteria(int _GDID)
            {
                this.GDID = _GDID;
                this.directDelete = false;
            }

            public RootCriteria(int _GDID, bool directDelete)
            {
                this.GDID = _GDID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int GDID;
            public bool directDelete = false;
            public ChildCriteria(int _GDID)
            {
                this.GDID = _GDID;
                this.directDelete = false;
            }

            public ChildCriteria(int _GDID, bool directDelete)
            {
                this.GDID = _GDID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DMGDBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[GDID] = " + criteria.GDID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DMGDBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["GDID"] is DBNull))
                this._GDID = (int)dataReader["GDID"];
            if (!(dataReader["NHGD"] is DBNull))
                this._NHGD = (int)dataReader["NHGD"];
            if (!(dataReader["VTRI"] is DBNull))
                this._VTRI = (int)dataReader["VTRI"];
            if (!(dataReader["LDID"] is DBNull))
                this._LDID = (int)dataReader["LDID"];
            if (!(dataReader["TDID"] is DBNull))
                this._TDID = (int)dataReader["TDID"];
            if (!(dataReader["GIAC"] is DBNull))
                this._GIAC = (double)dataReader["GIAC"];
            if (!(dataReader["HSCL"] is DBNull))
                this._HSCL = (bool)dataReader["HSCL"];
            if (!(dataReader["HSHT"] is DBNull))
                this._HSHT = (bool)dataReader["HSHT"];
            if (!(dataReader["HSDD"] is DBNull))
                this._HSDD = (bool)dataReader["HSDD"];
            if (!(dataReader["VBID"] is DBNull))
                this._VBID = (int)dataReader["VBID"];
            if (!(dataReader["GHIC"] is DBNull))
                this._GHIC = (string)dataReader["GHIC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DMGDBO_InsertBO");
            db.AddInParameter(cmd, "@NHGD", SqlDbType.Int, this._NHGD);
            db.AddInParameter(cmd, "@VTRI", SqlDbType.Int, this._VTRI);
            db.AddInParameter(cmd, "@LDID", SqlDbType.Int, this._LDID);
            db.AddInParameter(cmd, "@TDID", SqlDbType.Int, this._TDID);
            db.AddInParameter(cmd, "@GIAC", SqlDbType.Float, this._GIAC);
            db.AddInParameter(cmd, "@HSCL", SqlDbType.Bit, this._HSCL);
            db.AddInParameter(cmd, "@HSHT", SqlDbType.Bit, this._HSHT);
            db.AddInParameter(cmd, "@HSDD", SqlDbType.Bit, this._HSDD);
            db.AddInParameter(cmd, "@VBID", SqlDbType.Int, this._VBID);
            db.AddInParameter(cmd, "@GHIC", SqlDbType.NVarChar, this._GHIC);
            db.AddOutParameter(cmd, "@NewGDID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._GDID = (int)cmd.Parameters["@NewGDID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DMGDBO_UpdateBO");
            db.AddInParameter(cmd, "@GDID", SqlDbType.Int, this._GDID);
            db.AddInParameter(cmd, "@NHGD", SqlDbType.Int, this._NHGD);
            db.AddInParameter(cmd, "@VTRI", SqlDbType.Int, this._VTRI);
            db.AddInParameter(cmd, "@LDID", SqlDbType.Int, this._LDID);
            db.AddInParameter(cmd, "@TDID", SqlDbType.Int, this._TDID);
            db.AddInParameter(cmd, "@GIAC", SqlDbType.Float, this._GIAC);
            db.AddInParameter(cmd, "@HSCL", SqlDbType.Bit, this._HSCL);
            db.AddInParameter(cmd, "@HSHT", SqlDbType.Bit, this._HSHT);
            db.AddInParameter(cmd, "@HSDD", SqlDbType.Bit, this._HSDD);
            db.AddInParameter(cmd, "@VBID", SqlDbType.Int, this._VBID);
            db.AddInParameter(cmd, "@GHIC", SqlDbType.NVarChar, this._GHIC);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_GDID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DMGDBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@GDID", SqlDbType.Int, criteria.GDID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_GDID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
